﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Collections.ObjectModel;

namespace QuanLyDuocPham.Controllers.QuanLyHeThong.DanhMuc
{
    class DONVITINH_Control : IDisposable
    {
        DuocPhamDataContext db = new DuocPhamDataContext(staticClass.Conn);
        public void Dispose()
        {
            if (db != null)
            {
                db.Dispose();
                db = null;
            }
        }
        public void loadComboBox(ComboBox cmb)
        {

            cmb.ItemsSource = null;
            var types = db.DONVITINH_Get_List().Join(db.DONVITINH_Get_List(), p => p.DON_MA_DVT, c => c.MA_DVT, (p, c) => new { p.DIEN_GIAI,p.MA_DVT, TENB = c.DIEN_GIAI, p.SO_LUONG, MA_DVT_B = p.DON_MA_DVT });            

          var typeorder = types.OrderBy(w => w.DIEN_GIAI);
          foreach (var t in typeorder)
          {
              cmb.Items.Add(t);
          }
          
          cmb.SelectedValuePath = "MA_DVT";          
          cmb.IsTextSearchEnabled = true;
          cmb.IsEditable = true;
          
        }
        
        public string loadID_moi()
        {
            string ID = "";

            int rowcount = db.DONVITINH_Get_List().Count();
            if (rowcount == 0)
            {
                ID = "DVT0001";
            }
            else
            {
                for (int i = 1; i < rowcount+2; i++)
                {
                    string IDtam = "DVT";
                    for (int j = 0; j < (4 - i.ToString().Length); j++)
                        IDtam = IDtam + "0";
                    IDtam = IDtam + i;
                    Boolean kq = true;
                    var data = db.DONVITINH_Get_List();

                    foreach (var t in data)
                    {

                        if (t.MA_DVT.Trim() == IDtam.Trim())

                            kq = false;
                    }
                    if (kq == true)
                    {
                        ID = IDtam;
                        break;
                    }
                }

            }
            return ID;

        }
        public void addData(string ma_dvt, TextBox txtDien_Giai, TextBox txtQuyDoi, ComboBox cmbDon_Ma_DVT)
        {
            if (validateData(txtDien_Giai) == false)
                MessageBox.Show("Chưa nhập đủ thông tin!");
            else
            {
                
                db.DONVITINH_Insert(ma_dvt, txtDien_Giai.Text, cmbDon_Ma_DVT.SelectedValue.ToString(), Int32.Parse(txtQuyDoi.Text));
                MessageBox.Show("Đã thêm thành công");
            }
        }
        private bool validateData(TextBox txtDien_Giai)
        {
            if (txtDien_Giai.Text.Trim() == "" )
            {
                return false;
            }
            return true;
        }

        public void loadData(ListView List)
        {
            List.Items.Clear();

            var types = db.DONVITINH_Get_List().Join(db.DONVITINH_Get_List(), p => p.DON_MA_DVT, c => c.MA_DVT, (p, c) => new { MA_DVT = p.MA_DVT, TENA = p.DIEN_GIAI, TENB = c.DIEN_GIAI, SO_LUONG = p.SO_LUONG, MA_DVT_B = p.DON_MA_DVT });
            var typeorder = types.OrderBy(w => w.TENA);
            foreach (var t in typeorder)
            {
                List.Items.Add(t);
            }
        }

        public void loadData(DataPager dp)
        {

            ObservableCollection<Object> dpSource = new ObservableCollection<Object>();

            var types = db.DONVITINH_Get_List().Join(db.DONVITINH_Get_List(), p => p.DON_MA_DVT, c => c.MA_DVT, (p, c) => new { MA_DVT = p.MA_DVT, TENA = p.DIEN_GIAI, TENB = c.DIEN_GIAI, SO_LUONG = p.SO_LUONG, MA_DVT_B = p.DON_MA_DVT });
            var typeorder = types.OrderBy(w => w.TENA);
            int i = 1;
            foreach (var u in typeorder)
            {
                if (u.TENA.Trim() != "")
                {
                    dpSource.Add(new { STT = i, u.TENB, u.TENA, u.SO_LUONG, u.MA_DVT_B, u.MA_DVT });
                    i++;

                }
            }
            dp.ItemsSource = dpSource;
            dp.ItemsPerPage = 10;
        }
        public void gettype(ListView list)
        {
            var types = db.DONVITINH_Get_List().Join(db.DONVITINH_Get_List(), p => p.DON_MA_DVT, c => c.MA_DVT, (p, c) => new { MA_DVT = p.MA_DVT, TENA = p.DIEN_GIAI, TENB = c.DIEN_GIAI, SO_LUONG = p.SO_LUONG, MA_DVT_B = p.DON_MA_DVT });
            
        }

        public void updateData(string ma_dvt, TextBox txtDien_Giai, TextBox txtQuyDoi, ComboBox cmbDon_Ma_DVT)
        {
            if (validateData(txtDien_Giai) == false)
                MessageBox.Show("Chưa nhập đủ thông tin!");
            else
            {
                
                db.DONVITINH_Update(ma_dvt, ma_dvt, txtDien_Giai.Text, cmbDon_Ma_DVT.SelectedValue.ToString(), Int32.Parse(txtQuyDoi.Text));
                MessageBox.Show("Đã cập nhật!");
            }
        }

        public string deleteData(string ID_Delete, string error)
        {

            error = "";
            if (db.DUOCPHAM_GetByMaDvt(ID_Delete).Count() > 0 || db.DONVITINH_GetByDonMaDvt(ID_Delete).Count() > 0)
                error = "Đang có dược phẩm sử dụng đơn vị tính này, không thể xóa";
            else
            {
                db.DONVITINH_Delete(ID_Delete);
                error = "Xóa thành công!";
            }
            return error;

        }

        public void loadForm_Edit(string ID_Edit, TextBox txtDien_Giai, TextBox txtQuyDoi, ComboBox cmbDon_Ma_DVT)
        {

            var sl = (DONVITINH_GetByMaDvtResult)db.DONVITINH_GetByMaDvt(ID_Edit).Single();


            
            txtDien_Giai.Text = sl.DIEN_GIAI;           
            txtQuyDoi.Text = sl.SO_LUONG.ToString();
            loadComboBox(cmbDon_Ma_DVT);
            cmbDon_Ma_DVT.SelectedValue = sl.DON_MA_DVT;

            
        }

        public string[] get_item(ListView list1)
        {
            string[] pro;
            int i = list1.SelectedIndex;
            var types = db.DONVITINH_Get_List().Join(db.DONVITINH_Get_List(), p => p.DON_MA_DVT, c => c.MA_DVT, (p, c) => new { MA_DVT = p.MA_DVT, TENA = p.DIEN_GIAI, TENB = c.DIEN_GIAI, SO_LUONG = p.SO_LUONG, MA_DVT_B = p.DON_MA_DVT });

            var typeorder = types.OrderBy(w => w.TENA);
            var sl = typeorder.ElementAt(i);
            pro = new string[] { sl.MA_DVT, sl.TENA, sl.SO_LUONG.ToString(), sl.TENB };

            return pro;

        }
    }
}
